package com.mfdm.gui.wizards;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import com.mfdm.gui.utils.ColorAndFontUtils;

public class AddChordToSongWizardPage extends WizardPage {

	private static final int NUMBER_OF_WORD_LIMITED = 18;

	private static final String ADD_CHORDS_TO_SONG_CONTENT = "Add chords to song content";
	
	private static final String ENTER_SYMBOL = "\n";
	
	private static final String REGEX = " ";
	
	private Composite m_composite;
	
	private ScrolledComposite scrolledComposite;
	
	
	protected AddChordToSongWizardPage(String pageName) {
		super(pageName);
		setTitle(ADD_CHORDS_TO_SONG_CONTENT);
	}

	@Override
	public void createControl(Composite parent) {
		scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL|SWT.H_SCROLL);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		m_composite = new Composite(scrolledComposite, SWT.BORDER);
		
		scrolledComposite.setContent(m_composite);
		
		GridLayout gridLayout = new GridLayout(NUMBER_OF_WORD_LIMITED, false);
		m_composite.setLayout(gridLayout);		
		m_composite.setBackground(new Color(this.getShell().getDisplay(), 255, 255, 255));
		m_composite.setRedraw(true);
		
		scrolledComposite.setMinSize(m_composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		setControl(m_composite);
		
	}
	
	public void showContents(String songContent) {
		
		List<List<String>> listOfRows = getWordsDetailsFronComtent(songContent);
		if (listOfRows.size() > 0) {
			List<Control> listControls = new ArrayList<Control>();
			
			for (List<String> row : listOfRows) { // for every row
				if (row.size() > 0) { 
					for (int i = 0; i < row.size(); i++) { // draw combo
						Combo combo = new Combo(m_composite, SWT.NONE);
						combo.add("Am");
						combo.add("Dm");
						
						listControls.add(combo);
					}
					Label label = new Label(m_composite, SWT.NONE);
					label.setText("");
					label.setBackground(ColorAndFontUtils.getWhiteColor(m_composite));
					GridData gridData = new GridData(GridData.FILL, GridData.FILL,  true, true);
					gridData.horizontalSpan = NUMBER_OF_WORD_LIMITED - row.size();	
					label.setLayoutData(gridData);
					
					listControls.add(label);
					
									
					
					for (String word : row) {
						Label label2 = new Label(m_composite, SWT.NONE);
						label2.setText(word);
						label2.setFont(ColorAndFontUtils.getDefaultTextFont(m_composite));
						label2.setBackground(ColorAndFontUtils.getWhiteColor(m_composite));
						GridData gridDataText = new GridData(50, 30);
						label2.setLayoutData(gridDataText);
						
						listControls.add(label2);
					}
					Label label3 = new Label(m_composite, SWT.NONE);
					label3.setText("");
					label3.setBackground(ColorAndFontUtils.getWhiteColor(m_composite));
					GridData gridData2 = new GridData(GridData.FILL, GridData.FILL,  true, true);
					gridData2.horizontalSpan = NUMBER_OF_WORD_LIMITED - row.size();
					label3.setLayoutData(gridData2);
					
					listControls.add(label3);
				}
			}
			
			Control[] controls = new Control[listControls.size()];
			
			for (int i = 0; i < listControls.size(); i++) {
				controls[i] = listControls.get(i);
			}
			
			m_composite.changed(controls);
			scrolledComposite.setMinSize(m_composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
			m_composite.layout();
		}				
	}
	
	private List<List<String>> getWordsDetailsFronComtent(String songContent) {
		List<List<String>> results = new ArrayList<List<String>>();
		String[] rows = songContent.split(ENTER_SYMBOL);
		for (String row : rows) {
			
			String[] words = row.split(REGEX);
			if (words.length >= NUMBER_OF_WORD_LIMITED) {
				List<String> tempFirst = new ArrayList<String>();
				List<String> tempSecond = new ArrayList<String>();
				
				for (int i = 0; i < words.length; i++) {
					if (i < NUMBER_OF_WORD_LIMITED - 1) {
						tempFirst.add(words[i]);
					} else {
						tempSecond.add(words[i]);
					}
				}
				results.add(tempFirst);
				results.add(tempSecond);
				
			} else {		
				List<String> temp = new ArrayList<String>();
				for (String word : row.split(REGEX)) {
					if (!"\r".equals(word) && !"\n".equals(word)) {
						temp.add(word);
					}
				}
				results.add(temp);
			}			
		}	
		
		return results;		
	}

}
